package cn.csc.module.order.controller.admin.order;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.order.controller.admin.order.vo.OrderExcelVO;
import cn.csc.module.order.controller.admin.order.vo.OrderInfoRespVO;
import cn.csc.module.order.controller.admin.order.vo.OrderPageReqVO;
import cn.csc.module.order.controller.admin.order.vo.OrderRespAdminVO;
import cn.csc.module.order.convert.addr.OrderAddrConvert;
import cn.csc.module.order.convert.item.OrderItemConvert;
import cn.csc.module.order.convert.order.OrderConvert;
import cn.csc.module.order.dal.dataobject.addr.OrderAddrDO;
import cn.csc.module.order.dal.dataobject.item.OrderItemDO;
import cn.csc.module.order.dal.dataobject.order.OrderDO;
import cn.csc.module.order.service.addr.OrderAddrService;
import cn.csc.module.order.service.item.OrderItemService;
import cn.csc.module.order.service.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 订单信息")
@RestController
@RequestMapping("/order/")
@Validated
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private OrderAddrService orderAddrService;


    @GetMapping("/get")
    @ApiOperation("获得订单信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order::query')")
    public CommonResult<OrderInfoRespVO> get(@RequestParam("id") BigInteger id) {
        //订单详情信息，
        OrderDO orderDO  = orderService.get(id);
        OrderInfoRespVO orderInfoRespVO=OrderConvert.INSTANCE.convertInfo(orderDO);
        //订单项信息
        List<OrderItemDO> spuList= orderItemService.getItemByOrderId(id);       ;
        orderInfoRespVO.setItemVOList( OrderItemConvert.INSTANCE.convertList(spuList));
        //订单地址信息
        OrderAddrDO orderAddrDO=orderAddrService.getAddrByOrderId(id);
        orderInfoRespVO.setOrderAddr(OrderAddrConvert.INSTANCE.convert(orderAddrDO));
        return success(orderInfoRespVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得订单信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('order::query')")
    public CommonResult<List<OrderRespAdminVO>> getList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<OrderDO> list = orderService.getList(ids);
        return success(OrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得订单信息分页")
    @PreAuthorize("@ss.hasPermission('order::query')")
    public CommonResult<PageResult<OrderRespAdminVO>> getPage(@Valid OrderPageReqVO pageVO) {
        log.info("[getPage][pageVO]=={}",pageVO);
        PageResult<OrderRespAdminVO> pageResult = orderService.getPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出订单信息 Excel")
    @PreAuthorize("@ss.hasPermission('order::export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid OrderPageReqVO pageVO,
              HttpServletResponse response) throws IOException {
        List<OrderDO> list = orderService.getExportList(pageVO);
        // 导出 Excel
        List<OrderExcelVO> datas =new ArrayList<>();
        OrderExcelVO vo=null;
        for(OrderDO data:list){
            vo=OrderConvert.INSTANCE.convert02(data);
         //查询产品名称，规格
            List<OrderItemDO> spuList= orderItemService.getItemByOrderId( new BigInteger(vo.getId()));
            vo.setSkuName(spuList.get(0).getSkuName());
            vo.setPrice(spuList.get(0).getPrice());
            datas.add(vo);
        }
        ExcelUtils.write(response, "商城订单信息.xls", "数据", OrderExcelVO.class, datas);
    }


}
